import { HttpClient, HttpParams } from '@angular/common/http';
import { Inject, Injectable, Injector } from '@angular/core';
import { Observable } from 'rxjs';
import { API_CONFIG, ServicesModule } from './services.module';
import { map } from 'rxjs/internal/operators';
import { Singer, SingerDetail } from './data-types/common.types';
import * as queryString from 'query-string';

type SingerParams ={
  offset:number;
  limit:number;
  cat?: string;
}
const defaultParams: SingerParams ={
  offset:0,
  limit:9,
  cat:"5001"
}

@Injectable({
  providedIn: ServicesModule
})
export class SingerService {

  constructor(private http:HttpClient, @Inject(API_CONFIG) private uri:string ) { }

  // 获取入驻歌手
  getEnterSinger(args:SingerParams = defaultParams): Observable<Singer[]>{
    const params = new HttpParams({fromString: queryString.stringify(args)})
    return this.http.get(this.uri + 'artist/list',{params})
    .pipe(map((res:any) => res.artists));
  }

  // 获取歌手详情和热门歌曲
  getSingerDetail(id: string): Observable<SingerDetail>{
    const params = new HttpParams().set('id', id)
    return this.http.get(this.uri + 'artists',{params})
    .pipe(map((res:any) => res as SingerDetail));
  }

  // 获取相似歌手
  getSimiSinger(id: string): Observable<Singer[]>{
    const params = new HttpParams().set('id', id)
    return this.http.get(this.uri + 'simi/artist',{params})
    .pipe(map((res:any) => res.artists));
  }

}
